<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CoreServiceApp
 *
 * @author jianfeng
 */
class BServiceEngine extends BServiceApp {
    //operations

    const LOADSERVICEOPERATION = "loadService";
    const ADDSERVICEAPPOPERATION = "addServiceApp";
    const GETSERVICEAPPSOPERATION = "getServiceApps";
    const Operation_SendMessage = "sendMessage";
    const Operation_joinService = "joinService";

    private $operationList = array();

    public function registerAppRoles() {

        $operations = $this->operationList;
        $operations['AddServiceApp'] = "addServiceApp";
        $operations['UpdateServiceApp'] = "updateApp";

        $authTemplate1 = new BAuthTemplate1();

        $workTeamAdmin = $authTemplate1->workTeamAdmin;
        $workTeamAdmin['operations'] = $this->operationList;

        $appSuper = $authTemplate1->appSuper;
        //$appSuper['name'] = 'service_super';
        $appSuper['showname'] = 'service_super';

        $appAdmin = $authTemplate1->appAdmin;
        //$appAdmin['name'] = 'service_admin';
        $appAdmin['showname'] = 'service_admin';

        $appMember = $authTemplate1->appMember;
        //$appMember['name'] = 'service_member';
        $appMember['showname'] = 'service_member';

        $authManager = $this->getAuthManager();

        $authManager->createRecursiveRole($workTeamAdmin);
        $authManager->createRecursiveRole($authTemplate1->workTeamMember);
        $authManager->createRecursiveRole($authTemplate1->servedTeamAdmin);
        $authManager->createRecursiveRole($authTemplate1->servedTeamMember);
        $authManager->createRecursiveRole($appSuper);
        $authManager->createRecursiveRole($appAdmin);
        $authManager->createRecursiveRole($appMember);
    }

    public function updateApp() {
        
    }

    protected function init() {
        parent::init();
        $this->setDbAdapter(new BServiceEngineDb($this));
    }

    public function getServiceAppByOperation($operation) {
        $db = new BServiceEngineDb();

        $appClass = $db->getServiceAppClassByOperationId($operation);

        $app = new $appClass;

        return $app;
    }

    public function addRolesToService($service) {
        
    }

    /**
     *
     * @param type $BService
     * @param type $BServiceApp 
     */
    protected function addServiceApp($config = NULL) {

        $olineQue = new BOnlineQueue();
        $serviceId = NULL;
        $appId = NULL;

        if ($config != NULL) {
            $service = $config['Service'];
            $app = $config['App'];

            if (is_numeric($service))
                $serviceId = $service;
            if ($service instanceof BService)
                $serviceId = $service->getId();

            if (is_numeric($app))
                $appId = $app;
            if ($app instanceof BServiceApp)
                $appId = $app->getId();

            //$db = $this->getDbAdapter();
            
            if ($serviceId != NULL && $appId != NULL)
            {
                $app = BServiceApp::getInstanceById($appId);
                $app->addToService($serviceId);
                //$dbresult = $db->addServiceApp($serviceId, $appId);
            }

            //$app->addToService($service);
        }
    }

    protected function getServiceApps($config) {

        $service = $config['Service'];

        return $service->getServiceApps();
    }

    protected function getUserServiceApps($config) {
        $userApps = array();
        $user = $config['User'];
        $service = $config['Service'];

        $apps = $this->getServiceApps(array('Service' => $service));

        foreach ($apps as $app) {
            $roles = $app->getAuthManager()->getUserRoles($user, $service);

            if ($roles != null && !empty($roles)) {
                array_push($userApps, $app);
            }
        }

        return $userApps;
    }

    public function getServiceType($serviceId) {

        $db = $this->getDbAdapter();

        $type = $db->getServiceType($serviceId);

        return $type;
    }

    public function registerAppOperations() {

        /* return array(
          array('name' => 'CreateService', 'function' => 'createService', 'data' => array('screenflow' => 'createService')),
          array('name' => 'JoinService', 'function' => 'joinService', 'data' => array('screen' => 'BScreenJoinService')),
          array('name' => 'sendMessage', 'function' => 'showServiceMessage')
          );
         * 
         */
    }

    protected function showServiceMessage() {

        /*
          $msgRequests = array();
          $ocMessage = new BOCPersonalMessage();
          $opRequests = $ocMessage->getOperationRequests();

          //get operation request of message
          foreach ($opRequests as $opRequest) {
          if ($opRequest->operation == BOCPersonalMessage::Operation_SendMessage) {
          array_push($msgRequests, $opRequest);
          }
          }
         * 
         */
    }

    protected function addServiceMember($serviceId, $roleId, $userId) {
        $authManager = $this->getAuthManager();
        if (is_string($roleId))
            $roleId = $authManager->getRoleId($roleId);
        $result = $authManager->assignUserToRole($serviceId, $userId, $roleId);

        if ($result > 0)
            return TRUE;

        return FALSE;
    }

    //this operation will be call before render operation if opeation is 
    //UI enable opeation
    //operation call also call formm some code without UI show
    public function doOperation($operationName, $config = NULL) {

        parent::doOperation($operationName, $config);

        if ($operationName == self::Operation_SendMessage)
            return $this->showServiceMessage();
        if ($operationName == self::ADDSERVICEAPPOPERATION)
            return $this->addServiceApp($config);
        if ($operationName == self::GETSERVICEAPPSOPERATION)
            return $this->getServiceApps($config);
        if ($operationName == self::Operation_joinService) {

            if (is_array($config) && !empty($config)) {
                $serviceId = $config['serviceId'];
                $role = $config['applyingRole'];
                $userId = $config['userId'];
                return $this->addServiceMember($serviceId, $role, $userId);
            }
        }
    }

    public function getOperations($roles = NULL) {
        
    }

}

?>
